package com.xiao.hao.config;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.IOException;

@WebFilter
public class WebFilterConfig implements Filter {


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
            ServletException {
        HttpServletRequestWrapper requestWrapper = new
                HttpServletRequestWrapper((HttpServletRequest) request) {
                    @Override
                    public String getHeader(String name) {
                        // 先从原本的Request中获取头，如果为空且名字为token，则从参数中查找并返回
                        String superHeader = super.getHeader(name);
                        if ("Authorization".equals(name) && StringUtils.isEmpty(superHeader)) {
                            String token = request.getParameter("token");
                            if (StringUtils.isNotEmpty(token)) {
                                return token;
                            }
                        }
                        return superHeader;
                    }
                };
        chain.doFilter(requestWrapper, response);
    }

    @Override
    public void destroy() {
    }
}
